package com.ascainiao.paygame.controller;

import com.alibaba.fastjson.JSONObject;
import com.ascainiao.paygame.beans.ReturnData;
import com.ascainiao.paygame.mapper.GameMapper;
import com.ascainiao.paygame.model.Game;
import com.ascainiao.paygame.utils.TokenUtils;
import com.ascainiao.paygame.utils.Tools;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class GameController {
    @Autowired
    private GameMapper gameMapper;
    @PostMapping("/game_info")
    public ReturnData getGameInfo(){
        ReturnData rdata=new ReturnData();
        rdata.setStatusCode("fail");
        rdata.setMessage("失败");
        rdata.setData(null);
        List<Game> games = gameMapper.selectList(null);
        if(!games.isEmpty()){
            rdata.setStatusCode("success");
            rdata.setMessage("成功");
            rdata.setData(games);
        }
        return rdata;
    }
    @GetMapping("/get_game_config")
    public ReturnData getGameConfig(@RequestParam(name = "id", required = false) String id) throws IOException {
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<Game> games = gameMapper.selectList(queryWrapper);
        if(games.size()==1){
            Game gameModel = games.get(0);
            String hash = gameModel.getConfig();
            JSONObject jsonObject =Tools.readJsonFile("/uploads/"+hash+".json");
            return Tools.output("success","成功",jsonObject);
        }
        return Tools.output("fail","失败",null);
    }
    @PostMapping("/save_game")
    public ReturnData saveGame(@RequestParam int id,@RequestParam String config) throws IOException {
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<Game> games = gameMapper.selectList(queryWrapper);
        if(games.size()==1){
            Game gameModel = games.get(0);
            String hash = gameModel.getConfig();
            //JSONObject jsonObject =Tools.readJsonFile("/uploads/"+hash+".json");
            Tools.writeJsonFile("/uploads/"+hash+".json",config);
            return Tools.output("success","成功",null);
        }
        return Tools.output("fail","失败",null);
    }
    @PostMapping("/create_game")
    public ReturnData createGame(@RequestParam String name,@RequestParam String config,@RequestParam int sort){
        //QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        String hash= TokenUtils.generateToken(32);
        Game gameModel=new Game();
        gameModel.setName(name);
        gameModel.setConfig(hash);
        gameModel.setSort(sort);
        if(gameMapper.insert(gameModel)==1){
            Tools.writeJsonFile("/uploads/"+hash+".json",config);
            return Tools.output("success","成功",null);
        }
        return Tools.output("fail","失败",null);
    }
}
